package cn.zh.blog.controller.admin;

import cn.zh.blog.pojo.User;
import cn.zh.blog.service.BlogService;
import cn.zh.blog.service.TagService;
import cn.zh.blog.service.TypeService;
import cn.zh.blog.service.UserService;
import cn.zh.blog.util.RandomValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 郑豪
 * @Date 2020/4/6 22:57
 **/
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TypeService typeService;

    @GetMapping("")
    public String loginPage(){
        return "admin/login";
    }

    @GetMapping("/login")
    public String showPage(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        if (user != null){
            model.addAttribute("count",count());
            return "admin/index";
        }
        return "admin/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        @RequestParam String code,
                        HttpSession session,
                        RedirectAttributes attributes,
                        Model model){
        //从session中获取随机数
        String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
        if (random == null) {
            attributes.addFlashAttribute("message","请输入验证码 -_-!!!");
            return "redirect:/admin/login";
        }
        if (random.equals(code)) {
            User user = userService.checkUser(username, password);
            if (user != null) {
                user.setPassword(null);
                session.setAttribute("user",user);
                model.addAttribute("count",count());
                return "admin/index";
            } else {
                attributes.addFlashAttribute("message","用户名或密码错误 -_-!!!");
                return "redirect:/admin/login";
            }
        } else {
            attributes.addFlashAttribute("message","验证码错误 -_-!!!");
            return "redirect:/admin/login";
        }
    }

    private Map<String,Long> count() {
        Map<String,Long> count = new HashMap<>(4);
        count.put("user", userService.countUser());
        count.put("blog", blogService.countBlog());
        count.put("tag", tagService.countTag());
        count.put("type", typeService.countType());
        return count;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/admin/login";
    }
     @GetMapping("index")
    public String index(Model model){
         model.addAttribute("count", count());
       return "admin/index";
 }
    /**
     * 生成验证码
     */
    @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 设置相应类型,告诉浏览器输出的内容为图片
            response.setContentType("image/jpeg");
            // 设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            // 输出验证码图片方法
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
